package com.li.network.socket.TCP;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;


/**
 * @author li
 * @date ${DATE} ${TIME}
 */
public class Client {

    public static void main(String[] args) throws IOException {
        String host = "127.0.0.1";
        int port = 6789;
        //敲响欢迎之门
        try (Socket client = new Socket(host, port)) {
            //发送请求
            OutputStreamWriter writer = new OutputStreamWriter(client.getOutputStream());
            writer.write("Hello I'm Li");

            //读响应
            InputStreamReader reader = new InputStreamReader(client.getInputStream());
            char[] chars = new char[1024];
            StringBuilder builder = new StringBuilder();
            while (reader.read(chars) != -1) {
                builder.append(new String(chars, 0 , chars.length));
            }
            System.out.println("客户端收到响应：" + builder);
        }
    }
}